package cn.edu.scau.cmi.luochenghao.chapter10;
import java.io.*;
import java.nio.channels.*;
import java.util.Scanner;
public class Example10_14 {

	public static void main(String[] args) {
        File file = new File("/Users/luochenghao/git/201725110111_luo_chenghao/201725110111罗铖昊/src/cn/edu/scau/cmi/luochenghao/chapter10/Example10_14.java");
        Scanner scanner = new Scanner(System.in);
        try {
            RandomAccessFile input = new RandomAccessFile(file, "rw");
            FileChannel channel = input.getChannel();
            FileLock lock = channel.tryLock();
            System.out.println("输入要读取的行数:");
            while (scanner.hasNextInt()) {
                int m = scanner.nextInt();
                lock.release();
                for (int i = 1; i <= m; i++) {
                    String str = input.readLine();
                    System.out.println(str);
                }
                lock = channel.tryLock();
                System.out.println("输入要读取的行数:");
            }

        } 
        catch (IOException event) {
            System.out.println(event);
        }
    }

}
